package com.example.demo.Controller;

import com.example.demo.Dto.BannedDto;
import com.example.demo.Dto.LogoffDto;
import com.example.demo.Dto.UserUpdateDto;
import com.example.demo.Service.UserService;
import com.example.demo.Util.ThreadLocallUtil;
import com.example.demo.VO.UserInfoVo;
import com.example.demo.po.User;
import com.example.demo.result.ResponseResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/api/v1/users")
public class UsersController {
    @Autowired
    private UserService userService;


    /**
     * 获取用户信息
     * @return
     */
    @GetMapping
    public ResponseResult logout(){
        User user = ThreadLocallUtil.getUser();

        UserInfoVo getuser = userService.getuser(user);
        return new ResponseResult().ok(getuser);
    }
    /**
     * 删除用户
     * @param logoffDto
     * @return
     */
    @DeleteMapping
    public ResponseResult delect(@RequestBody LogoffDto logoffDto){
        userService.delect(logoffDto);
        return new ResponseResult().ok();
    }
    /**
     * 修改用户信息
     * @return
     */
    @PutMapping
    public ResponseResult put(@RequestBody UserUpdateDto userUpdateDto){
        if (userUpdateDto==null){
            return new ResponseResult().error(400,"参数错误");
        }
        userService.updatestatus(userUpdateDto,ThreadLocallUtil.getUser().getUserId());
        return new ResponseResult().ok();
    }
    /**
     * 管理员封禁用户
     * @param bannedDto
     * @return
     */
    @DeleteMapping("/admin")
    public ResponseResult fonjuser(@RequestBody BannedDto bannedDto){
        if (bannedDto==null){
            return new ResponseResult().error(400,"参数错误");
        }
        userService.updatestatus(bannedDto);
        return new ResponseResult().ok(null,"用户封禁成功");
    }
}
